package com.android.hedgehog;

import android.graphics.Color;

import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Input;

public class RacerScreen extends GameScreen {
	private enum GameState {
		Ready, Right, Left, Crash
	}

	Graphics graphics;
	float time;
	float lineBuffer = 0.0f;
	GameState state = GameState.Ready;
	float timeBuffer = 0.0f;
	float timeBuffer2 = 0.0f;
	float timeBuffer3 = 1.0f;
	int xPosition = 160;
	float endBuffer = 0;

	public RacerScreen(Graphics g, int difficulty, double currentTime) {
		graphics = g;

	}

	@Override
	public int update(int X, int Y, int totalScore, double time) {
		this.time = (float) time;

		if (state == GameState.Ready) {
			if (X > 0 && X < 160)
				state = GameState.Left;
			if (X > 160 && X < 320)
				state = GameState.Right;
		}

		if (state == GameState.Left) {
			if (X > 160 && X < 320)
				state = GameState.Ready;
		}
		if (state == GameState.Right) {
			if (X > 0 && X < 160)
				state = GameState.Ready;
		}

		return totalScore;
	}

	@Override
	public void present() {
		lineBuffer = ((float) 35 * time % 40.0f);

		if (state == GameState.Ready) {
			graphics.drawPixmap(Assets.racerbackgroud, 0, 31, xPosition, 0,
					320, 459);

			for (int j = 31; j < 480; j += 40)
				graphics.drawLine(0, j + lineBuffer, 320, j + lineBuffer,
						Color.WHITE);
			drawDigits();
			
			graphics.drawPixmap(Assets.car, 140, 380, 0, 0, 40, 59);
		}
		if (state == GameState.Left) {
			xPosition--;
			if (xPosition < 10) {
				state = GameState.Crash;
				endBuffer = lineBuffer;
			}
			graphics.drawPixmap(Assets.racerbackgroud, 0, 31, xPosition, 0,
					320, 459);
			for (int j = 31; j < 480; j += 40)
				graphics.drawLine(0, j + lineBuffer, 320, j + lineBuffer,
						Color.WHITE);
			drawDigits();
			graphics.drawPixmap(Assets.car, 140, 380, 80, 0, 40, 59);
		}
		if (state == GameState.Right) {
			xPosition++;
			if (xPosition > 310) {
				state = GameState.Crash;
				endBuffer = lineBuffer;
			}
			graphics.drawPixmap(Assets.racerbackgroud, 0, 31, xPosition, 0,
					320, 459);
			for (int j = 31; j < 480; j += 40)
				graphics.drawLine(0, j + lineBuffer, 320, j + lineBuffer,
						Color.WHITE);
			drawDigits();
			graphics.drawPixmap(Assets.car, 140, 380, 200, 0, 40, 59);

		}
		if (state == GameState.Crash) {
			graphics.drawPixmap(Assets.racerbackgroud, 0, 31, xPosition, 0,
					320, 459);
			for (int j = 31; j < 480; j += 40)
				graphics.drawLine(0, j + endBuffer, 320, j + endBuffer,
						Color.WHITE);
			//drawDigits();
			graphics.drawPixmap(Assets.car, 140, 380, 0, 0, 40, 59);
		}

	}

	public void drawDigits() {
		graphics.drawSpecialText("1", 200-xPosition, 15*time, 24, 1,Color.WHITE);
	}

}
